home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / MiniExamples / AskMe / Localization.m < prev    next >
Text File  |  1992-12-13  |  7KB  |  243 lines

  1. /*
  2.  * You may freely copy, distribute, and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  4.  * fitness for any particular use.
  5.  * 
  6.  */
  7.  
  8. #import <appkit/Application.h>
  9. #import <appkit/NXImage.h>
  10. #import <appkit/nextstd.h>
  11. #import <objc/NXStringTable.h>
  12. #import <defaults/defaults.h>
  13. #import <libc.h>
  14. #import "Localization.h"
  15.  
  16. #define NATIVE_LANGUAGE "English"
  17. static char *launchDir = NULL;    /* if this is NULL, it will be calculated */
  18.  
  19. /*
  20.  * launchDirOK will return a NULL launchDir if you launch from a Terminal Shell. 
  21.  */
  22. static BOOL launchDirOk()
  23. {
  24.     const char *slash;
  25.  
  26.     if (launchDir) return YES;
  27.  
  28.     slash = strrchr(NXArgv[0], '/');
  29.     if (slash && slash-NXArgv[0]) {
  30.     launchDir = malloc((slash-NXArgv[0]+1)*sizeof(char));
  31.     strncpy(launchDir, NXArgv[0], slash-NXArgv[0]);
  32.     launchDir[slash-NXArgv[0]] = '\0';
  33.     return YES;
  34.     }
  35.  
  36.     return NO;
  37. }
  38.  
  39. void InitLocalDateAndTime()
  40. {
  41.     time_t t;
  42.     char buffer[16];
  43.     t = time(NULL);
  44.     strftime(buffer, 15, "%H:%M", localtime(&t));
  45. }
  46.  
  47.         
  48. void LocalDateAndTime(char *buffer,int  maxBufferSize, const time_t *time) 
  49. {
  50.     const char *format;
  51.     
  52.     format = NXGetDefaultValue([NXApp appName], "NXDateAndTime");
  53.     if (format == NULL)
  54.         format = "%a %b %d %H:%M:%S %Z %Y";
  55.     strftime(buffer,maxBufferSize,format,localtime(time));
  56. }
  57.  
  58. void LocalDate(char *buffer,int  maxBufferSize, const time_t *time)
  59. {
  60.     const char *format;
  61.     
  62.     format = NXGetDefaultValue([NXApp appName], "NXDate");
  63.     if (format == NULL)
  64.         format = "%a %b %d %Y";
  65.     strftime(buffer,maxBufferSize,format,localtime(time));
  66. }
  67.  
  68. void LocalTime(char *buffer,int maxBufferSize, const time_t *time)
  69. {
  70.     const char *format;
  71.     
  72.     format = NXGetDefaultValue([NXApp appName], "NXTime");
  73.     if (format == NULL)
  74.         format = "%H:%M:%S %Z";
  75.     strftime(buffer,maxBufferSize,format,localtime(time));
  76. }
  77.  
  78.  
  79. /* If the user has not set a language preference, load the nib
  80.  * section stored in the Mach-O by default. Otherwise,
  81.  * the nib file loaded would follow the language set in the Preferences
  82.  * Application.
  83.  */
  84. id LoadLocalNib(const char *nibFile, id owner)
  85. {
  86.     BOOL found = NO;
  87.     id retval = nil;
  88.     const char *const *languages;
  89.     char path[MAXPATHLEN+1];
  90.  
  91.     languages = [NXApp systemLanguages];
  92.     if (languages && launchDirOk()) {
  93.     while (!found && *languages) {
  94.         if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
  95.         sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, nibFile);
  96.         if (!access(path, R_OK)) {
  97.         retval = [NXApp loadNibFile:path owner:owner withNames:NO];
  98.         found = YES;
  99.         }
  100.     languages++;
  101.     }
  102.     }
  103.  
  104.     return found ? retval : [NXApp loadNibSection:nibFile owner:owner withNames:NO];
  105. }
  106.  
  107.  
  108.  
  109. id LocalImage(const char *file)
  110. {
  111.     id retval = nil;
  112.     const char *const *languages;
  113.     char path[MAXPATHLEN+1];
  114.  
  115.     languages = [NXApp systemLanguages];
  116.     if (languages) {
  117.     while (!retval && *languages) {
  118.         if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
  119.         sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
  120.         retval = [NXImage newFromFile:file];
  121.         languages++;
  122.     }
  123.     } else {
  124.     retval = [NXImage newFromSection:file];
  125.     }
  126.  
  127.     return retval;
  128. }
  129.  
  130. /* If the user has not selected a language preference,
  131.  * return the key by default. 
  132.  * NOTE: The key is in the same language as the NATIVE_LANGUAGE.
  133.  */
  134.  
  135. const char *doLocalString(const char *tableName, const char *key, const char *value)
  136. {
  137.     id table = nil;
  138.     const char *tableValue;
  139.     const char *const *languages;
  140.     BOOL isMyAppsLanguage = NO;
  141.     char buffer[MAXPATHLEN+1];
  142.     static int debugLanguages = -1;
  143.     static id stringTableTable = nil;
  144.  
  145.     if (!(languages = [NXApp systemLanguages])) return value ? value : key;
  146.  
  147.     if (!tableName) tableName = [NXApp appName];
  148.     if (tableName[0] == '"' && tableName[strlen(tableName)-1] == '"') {
  149.     strcpy(buffer, tableName+1);
  150.     buffer[strlen(buffer)-1] = '\0';
  151.     tableName = NXUniqueString(buffer);
  152.     }
  153.  
  154.     if (debugLanguages < 0) debugLanguages = NXGetDefaultValue([NXApp appName], "NXDebugLanguages") ? 1 : 0;
  155.  
  156.     if (!(table = [stringTableTable valueForKey:tableName])) {
  157.     while (!table && *languages) {
  158.         if (!strcmp(*languages, NATIVE_LANGUAGE)) {
  159.         isMyAppsLanguage = YES;
  160.         break;
  161.         }
  162.         sprintf(buffer, "%s/%s.lproj/%s.strings", launchDir, *languages, tableName);
  163.         table = [NXStringTable newFromFile:buffer];
  164.         languages++;
  165.     }
  166.     if (!table) {
  167.         if (debugLanguages && !isMyAppsLanguage) NXLogError("Cannot parse '%s' strings.", tableName);
  168.         table = [NXStringTable new];
  169.     }
  170.     if (!stringTableTable) stringTableTable = [HashTable newKeyDesc:"*"];
  171.     [stringTableTable insertKey:tableName value:table];
  172.     }
  173.  
  174.     tableValue = [table valueForStringKey:key];
  175.  
  176.     if (!tableValue) {
  177.     if (debugLanguages && !isMyAppsLanguage)
  178.         NXLogError("Cannot find value for string '%s' in table '%s'.", key, tableName);
  179.     tableValue = value ? value : key;
  180.     [table insertKey:key value:(void *)value];
  181.     }
  182.  
  183.     return tableValue;
  184. }
  185.  
  186. /* If the application is launched from the Workspace, NXArgv[0] will contain the full pathname of the executable file. If you launch the application from a terminal command line, NXArgv[0] will contain only the filename specified in the command. To get from that file name to the absolute path of its directory, you can use a combination of chdir() and getwd().
  187. */
  188.  
  189. void getAppDirectory (char *appDirectory)
  190. {
  191.     char *suffix;
  192.  
  193.     strcpy (appDirectory,NXArgv[0]);
  194.     if (suffix = rindex(appDirectory,'/'))    
  195.         *suffix  = '\0';     /* remove executable name */
  196.     if (appDirectory) chdir(appDirectory); 
  197.     getwd(appDirectory);
  198.      
  199. }
  200.  
  201.  
  202. /* This method is specific to the AskMe application. It retrieves the proper
  203.  * AskMeText folder depending on which language preference has been set
  204.  * by the user.
  205.  * If the user has not set a language preference,the default language
  206.  * "English" will be used.
  207.  * Also, getAppDirectory is used to find the full directory path even
  208.  * if the user launches the app from the terminal shell.
  209.  */
  210. const char * findLocalDir()
  211. {
  212.     char path[MAXPATHLEN+1], appDirectory[MAXPATHLEN+1];
  213.     const char *dirName, *fullDirName;
  214.     const char *const *languages;
  215.     BOOL  found = NO;
  216.     
  217.         
  218.     dirName = LocalString( "AskMeText", NULL,
  219.                              "Directory where text files are stored ");
  220.     getAppDirectory(appDirectory);
  221.         
  222.     languages = [NXApp systemLanguages];
  223.     if (languages) {
  224.     while (!found && *languages) {
  225.         sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, dirName);
  226.         if (!access(path, R_OK)) {
  227.         found = YES;
  228.         }
  229.         languages++;
  230.     }
  231.     } 
  232.     if (!found) {
  233.     sprintf(path, "%s/%s.lproj/%s", appDirectory,NATIVE_LANGUAGE, dirName); 
  234.     }
  235.     
  236.  
  237.     fullDirName = NXUniqueString(path);    
  238.     return fullDirName;
  239.  
  240. }
  241.  
  242.  
  243.